<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Glaccount_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    public function save_gl_account($data){
      $this->db->query('call gl_accounts_dml(?,?,?,?,?,?,?,?,?,@pid,@err,@msg)',$data);
      $que = $this->db->query("SELECT @pid as lid, @err as err,@msg as msg");
      $msg; $err; $lid;
      foreach($que->result() as $keys){
        $err = $keys->err; $msg = $keys->msg;$lid = $keys->lid;
      }
      echo json_encode(array('err'=>$err,'msg'=>$msg,'lid'=>$lid));
    }

    public function edit_gl_account($data){
      //$que = $this->db->query("SELECT @pid as lid, @err as err,@msg as msg");
      $this->db->query('call gl_accounts_dml(?,?,?,?,?,?,?,?,?,@pid,@err,@msg)',$data);
      $que = $this->db->query("SELECT @pid as lid, @err as err,@msg as msg");
      $msg; $err; $lid;
      foreach($que->result() as $keys){
        $err = $keys->err; $msg = $keys->msg;$lid = $keys->lid;
      }
      echo json_encode(array('err'=>$err,'msg'=>$msg,'lid'=>$lid));
    }
    // $acode,$aname,$atype,$stype,$eflag
    public function searchGL($acode,$aname,$atype,$stype,$eflag){
        $stmt = "SELECT ga.account_id, ga.company_id, ga.acct_code, ga.acct_name,alv1.meaning acct_type_meaning, ga.acct_type , ga.sub_type, alv.meaning sub_type_meaning, ga.enable_flag FROM gl_accounts AS ga INNER JOIN app_lookup_values AS alv on alv.lookup_code = ga.sub_type INNER JOIN app_lookup_values AS alv1 on alv1.lookup_code = ga.acct_type";

        if($acode != "" && $aname == "" && $atype == ""  && $stype == "" && $eflag != ""){

          $sql = $stmt."  WHERE acct_code LIKE '".$acode."' AND ga.enable_flag LIKE '".$eflag."'"; //formcode

        }else if($acode == "" && $aname == "" && $atype == ""  && $stype != "" && $eflag != ""){

          $sql = $stmt."  WHERE sub_type LIKE '".$stype."' AND ga.enable_flag LIKE '".$eflag."'"; //exe file name

        }else if($acode == "" && $aname == "" && $atype != ""  && $stype == "" && $eflag != ""){

          $sql = $stmt."  WHERE acct_type LIKE '".$atype."' AND ga.enable_flag LIKE '".$eflag."'"; //module name

        }else if($acode == "" && $aname != "" && $atype == ""  && $stype == "" && $eflag != ""){

          $sql = $stmt."  WHERE acct_name LIKE '".$aname."' AND ga.enable_flag LIKE '".$eflag."'"; //formname

        }else if($acode == "" && $aname == "" && $atype == ""  && $stype == "" && $eflag != ""){

          $sql = $stmt."  WHERE ga.enable_flag LIKE '".$eflag."' AND ga.enable_flag LIKE '".$eflag."'"; //enable_flag

        }else if($acode != "" && $aname != "" && $atype == ""  && $stype == "" && $eflag != ""){

          $sql = $stmt."  WHERE acct_name LIKE '".$aname."' AND acct_code LIKE '".$acode."' AND ga.enable_flag LIKE '".$eflag."'"; //formcode , formname

        }else if($acode != "" && $aname != "" && $atype != ""  && $stype == "" && $eflag != ""){

          $sql = $stmt."  WHERE acct_name LIKE '".$aname."' AND acct_type LIKE '".$atype."' AND acct_name LIKE '".$aname."' AND acct_code LIKE '".$acode."' AND ga.enable_flag LIKE '".$eflag."'"; //formcode, formname, module name

        }else if($acode != "" && $aname != "" && $atype != ""  && $stype != "" && $eflag != ""){

          $sql = $stmt."  WHERE acct_name LIKE '".$aname."' AND acct_type LIKE '".$atype."' AND acct_code LIKE '".$acode."' AND sub_type LIKE '".$stype."' AND ga.enable_flag LIKE '".$eflag."' "; //formcode, formname, module name, exefilename

        }else if($acode != "" && $aname != "" && $atype == ""  && $stype != "" && $eflag != ""){

          $sql = $stmt."  WHERE acct_name LIKE '".$aname."' AND acct_code LIKE '".$acode."' AND sub_type LIKE '".$stype."'  AND ga.enable_flag LIKE '".$eflag."'"; //formcode, formname, exefilename

        }else if($acode != "" && $aname == "" && $atype == ""  && $stype != "" && $eflag != ""){

          $sql = $stmt."  WHERE acct_code LIKE '".$acode."' AND sub_type LIKE '".$stype."' AND ga.enable_flag LIKE '".$eflag."' "; //formcode exefilename

        }else if($acode != "" && $aname == "" && $atype != ""  && $stype != "" && $eflag != ""){

          $sql = $stmt."  WHERE acct_type LIKE '".$atype."' AND acct_code LIKE '".$acode."' AND sub_type LIKE '".$stype."'  AND ga.enable_flag LIKE '".$eflag."'"; //formcode, formname, module name, exefilename

        }else if($acode != "" && $aname == "" && $atype != ""  && $stype == "" && $eflag != ""){

          $sql = $stmt."  WHERE acct_type LIKE '".$atype."' AND acct_code LIKE '".$acode."' AND ga.enable_flag LIKE '".$eflag."'"; //formcode, formname, module name, exefilename

        }else if($acode == "" && $aname != "" && $atype == ""  && $stype != "" && $eflag != ""){

          $sql = $stmt."  WHERE acct_name LIKE '".$aname."' AND sub_type LIKE '".$stype."' AND ga.enable_flag LIKE '".$eflag."'"; //formcode, formname, module name, exefilename

        }else if($acode == "" && $aname != "" && $atype != ""  && $stype == "" && $eflag != ""){

          $sql = $stmt."  WHERE acct_name LIKE '".$aname."' AND acct_type LIKE '".$atype."' AND ga.enable_flag LIKE '".$eflag."'"; //formcode, formname, module name, exefilename

        }
          
        $query = $this->db->query($sql);
        if($query->num_rows() != 0){
          return $query->result();
        }
        else {
          $result = "No results found.";
          return $result;
        }
    }

    public function getGLdtl($id){
      $this->db->where('account_id',$id);
      $que = $this->db->get('gl_accounts_v');
      return $que->result();
    }
}